<?php

declare(strict_types=1);

namespace app\service\user\login;

use InvalidArgumentException;

class LoginFactory
{
    /**
     * 登录方式映射
     * @var array
     */
    private static $map = [
        'password' => PhonePassword::class,
        'sms' => PhoneCode::class,
        'oauth' => OAuth::class,
        'platform' => Platform::class,
        'pltform' => Platform::class,
    ];

    /**
     * 创建登录实例
     * @param string $type 登录方式
     * @return LoginInterface
     */
    public static function create(string $type): LoginInterface
    {
        if (!isset(self::$map[$type])) {
            throw new InvalidArgumentException("不支持的登录方式: {$type}");
        }

        $class = self::$map[$type];
        return app()->make($class);
    }

    /**
     * 注册新的登录方式
     * @param string $type 登录方式标识
     * @param string $class 实现类
     */
    public static function register(string $type, string $class): void
    {
        if (!is_subclass_of($class, LoginInterface::class)) {
            throw new InvalidArgumentException("登录实现类必须实现 LoginInterface 接口");
        }

        self::$map[$type] = $class;
    }
}
